package com.jieshuibao.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.jieshuibao.mapper.AdvisoryMapper;
import com.jieshuibao.mapper.AdvisoryRecordingExMapper;
import com.jieshuibao.mapper.AdvisoryRecordingMapper;
import com.jieshuibao.mapper.AdvisoryUserMapper;
import com.jieshuibao.mapper.UserDealRecordMapper;
import com.jieshuibao.mapper.UserMapper;
import com.jieshuibao.model.db.Advisory;
import com.jieshuibao.model.db.AdvisoryRecording;
import com.jieshuibao.model.db.AdvisoryRecordingExample;
import com.jieshuibao.model.db.AdvisoryUser;
import com.jieshuibao.model.db.User;
import com.jieshuibao.model.db.UserDealRecord;
import com.jieshuibao.model.domain.AdvisoryRecordingUser;
import com.jieshuibao.util.JsonUtil;
import com.jieshuibao.util.MoneyRandom;
import com.jieshuibao.util.PayTypeUtil;
import com.jieshuibao.util.UUIDGenerator;
import com.jieshuibao.wx.util.PayCommonUtil;

@Service
public class AdvisoryRecordingService{
	private static final Logger logger = Logger.getLogger(AdvisoryRecordingService.class);
	
	@Resource
	private AdvisoryRecordingMapper advisoryRecordingMapper;
	
	@Resource
	private AdvisoryRecordingExMapper advisoryRecordingExMapper;
	
	@Resource
	private AdvisoryUserMapper advisoryUserMapper;
	
	@Resource
	private AdvisoryMapper advisoryMapper;
	
	@Resource
	private UserMapper userMapper;
	
	@Resource
	private UserDealRecordMapper userDealRecordMapper; 
	
	public void addAdvisoryRecording(AdvisoryRecording advisoryRecording) {
		advisoryRecordingMapper.insertSelective(advisoryRecording);
	}
	
	public void delAdvisoryRecordingByUuid(String uuid) {
		advisoryRecordingMapper.deleteByPrimaryKey(uuid);
	}
	
	public void updateAdvisoryRecording(AdvisoryRecording advisoryRecording) {
		advisoryRecordingMapper.updateByPrimaryKeySelective(advisoryRecording);
	}
	
	public AdvisoryRecording getAdvisoryRecordingByUuid(String uuid) {
		return advisoryRecordingMapper.selectByPrimaryKey(uuid);
	}
	
	public List<AdvisoryRecording> getPageAdvisoryRecording(AdvisoryRecordingExample advisoryRecordingExample) {
		return advisoryRecordingMapper.selectPage(advisoryRecordingExample);
	}
	
	public int countAdvisoryRecording(AdvisoryRecordingExample advisoryRecordingExample) {
		return advisoryRecordingMapper.countByExample(advisoryRecordingExample);
	}
	public List<AdvisoryRecordingUser> getAdvisoryRecordingUsers(AdvisoryRecordingExample example){
		return advisoryRecordingExMapper.selectAdvisoryRecordongUserPage(example);
	}
	
	//结束业务
	public void addAdvisoryRecordUser(AdvisoryRecording advisoryRecording,AdvisoryUser advisoryUser1,AdvisoryUser advisoryUser2,Advisory advisory,String phoneNumber,int money){
		if(advisoryRecording!=null){
			advisoryRecordingMapper.insertSelective(advisoryRecording);
		}
		if(advisoryUser1!=null){
			advisoryUserMapper.updateByPrimaryKeySelective(advisoryUser1);
		}
		if(advisoryUser2!=null){
			advisoryUserMapper.updateByPrimaryKeySelective(advisoryUser2);
		}
		if(advisory!=null){
			String type = advisory.getType();
			String uuid = advisory.getUuid();
			
			if(type.equals("1")){
				//分钱记流水
				String uuid1 = UUIDGenerator.getUUID();
				String uuid2 = UUIDGenerator.getUUID();
				UserDealRecord userDealRecord1=new UserDealRecord();
				UserDealRecord userDealRecord2=new UserDealRecord();
				userDealRecord1.setUuid(uuid1);//用户收入
				userDealRecord2.setUuid(uuid2);//解税宝公司支出
				
				
				userDealRecord1.setAccountType(PayTypeUtil.ACCOUNTTYPE_PERSONAL);
				userDealRecord2.setAccountType(PayTypeUtil.ACCOUNTTYPE_BUSINESS);
				
				userDealRecord2.setUserId(PayTypeUtil.COMPANY_ACCPUNT);
				userDealRecord1.setUserId(phoneNumber);
				
				Date date = new Date();
				double advisorySubMoney = MoneyRandom.getAdvisorySubMoney(money);
				userDealRecord1.setBusinessNo(uuid);
				userDealRecord2.setBusinessNo(uuid);
				userDealRecord1.setCreateDate(date);
				userDealRecord2.setCreateDate(date);
				userDealRecord1.setSum(advisorySubMoney);
				userDealRecord2.setSum(advisorySubMoney);
				
				userDealRecord1.setGoodsType(PayTypeUtil.GOODSTYPE_ADVISORY);
				userDealRecord2.setGoodsType(PayTypeUtil.GOODSTYPE_ADVISORY);
				
				userDealRecord1.setTradeType(PayTypeUtil.TRDETYPE_INCOME);
				userDealRecord2.setTradeType(PayTypeUtil.TRDETYPE_EXPENDITURE);
				userDealRecord1.setPaySucTime(date);
				userDealRecord2.setPaySucTime(date);
				
				userDealRecord1.setStatus(PayCommonUtil.PAYSUCCESS_STATUS);
				userDealRecord2.setStatus(PayCommonUtil.PAYSUCCESS_STATUS);
				
				userDealRecord1.setPayType(PayTypeUtil.PAYTYPE_BALANCE);
				userDealRecord2.setPayType(PayTypeUtil.PAYTYPE_COMPANYENVELOPES);
				
				User user = userMapper.selectByPrimaryKey(phoneNumber);
				if(user!=null){
					Double balance = user.getBalance();
					User user2 = new User();
					user2.setBalance(balance+advisorySubMoney);
					user2.setPhoneNumber(phoneNumber);
					userMapper.updateByPrimaryKeySelective(user2);
					logger.info("一对一咨询 给咨询师分钱："+JsonUtil.toJson(user2));
					userDealRecordMapper.insertSelective(userDealRecord1);
					logger.info("一对一咨询 咨询师分钱流水："+JsonUtil.toJson(userDealRecord1));
					userDealRecordMapper.insertSelective(userDealRecord2);
					logger.info("一对一咨询 解税宝分钱流水："+JsonUtil.toJson(userDealRecord2));
				}
				
			}
			
			advisoryMapper.updateByPrimaryKeySelective(advisory);
			
			
			
		}
	}
}
